package com.ruoyi.wechat.api;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.degree.domain.BizComments;
import com.ruoyi.degree.domain.WechatNotifyRemind;
import com.ruoyi.degree.domain.WechatUser;
import com.ruoyi.degree.service.IBizCommentsService;
import com.ruoyi.degree.service.IWechatNotifyRemindService;
import com.ruoyi.degree.service.IWechatUserService;
import com.ruoyi.wechat.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wxapi/usernotify")
@Api(tags = "用户消息API", description = "消息相关接口")
public class WechatUserNotifyApi {
    @Autowired
    private IWechatNotifyRemindService IWechatNotifyRemindService;
    @Autowired
    private  IWechatUserService IWechatUserService;
    @Autowired
    private IBizCommentsService IBizCommentsService;
    @ApiOperation("消息未读计数")
    @Anonymous
    @GetMapping("/countNotReadNotify")
    public Long countNotReadNotify(Long userId) {

        return IWechatNotifyRemindService.countNotReadNotifyById(userId);
    }
    @ApiOperation("消息查找")
    @Anonymous
    @GetMapping("/selectNotifyList")
    public AjaxResult  selectNotifyList(Long userId) {
        List<MessageVO> messageVOList = new ArrayList<>();
       WechatNotifyRemind wechatNotifyRemind = new WechatNotifyRemind();
        wechatNotifyRemind.setUserId(userId);
        List<WechatNotifyRemind> notifyList = IWechatNotifyRemindService.selectWechatNotifyRemindList(wechatNotifyRemind);
        for (WechatNotifyRemind wechatNotifyRemind1 : notifyList) {
           MessageVO messageVO = new MessageVO();

            WechatUser wechatUser = IWechatUserService.selectWechatUserById(wechatNotifyRemind1.getUserId());
            if (wechatUser != null) {
                messageVO.setAvatarUrl(wechatUser.getAvatarUrl());
                messageVO.setUserName(wechatUser.getNickname());
            }

            BizComments bizComments = IBizCommentsService.selectBizCommentsById(wechatNotifyRemind1.getObjectId());
            if (bizComments != null) {
                messageVO.setContent(bizComments.getContent());
            }
            messageVO.setCreatedAt(wechatNotifyRemind1.getCreatedAt());
            messageVO.setSenderAction(wechatNotifyRemind1.getSenderAction());
            messageVO.setId(wechatNotifyRemind1.getId());
            messageVO.setReadAt(wechatNotifyRemind1.getReadAt());
            messageVO.setObjectId(wechatNotifyRemind1.getObjectId());
            messageVOList.add(messageVO);
        }

        return AjaxResult.success(messageVOList);
    }

    @ApiOperation("删除消息")
    @Anonymous
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return AjaxResult.success(IWechatNotifyRemindService.deleteWechatNotifyRemindById(id));
    }

    @ApiOperation("已读消息")
    @Anonymous
    @PutMapping("/read/{id}")
    public AjaxResult read(@PathVariable Long id) {
        WechatNotifyRemind wechatNotifyRemind = IWechatNotifyRemindService.selectWechatNotifyRemindById(id);
        if (wechatNotifyRemind != null) {
            Date now = new Date();
            wechatNotifyRemind.setReadAt(now);
            IWechatNotifyRemindService.updateWechatNotifyRemind(wechatNotifyRemind);
            return AjaxResult.success(now); // 当前时间放到 data 字段
        } else {
            return AjaxResult.error("消息不存在");
        }
    }
}
